package com.ruicar.afs.cloud.channel.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.ruicar.afs.cloud.channel.common.condition.ChannelAffiliatedToCaseCondition;
import com.ruicar.afs.cloud.channel.common.feign.ChannelUseApplyService;
import com.ruicar.afs.cloud.channel.common.feign.ChannelUseCaseService;
import com.ruicar.afs.cloud.channel.common.service.CommonService;
import com.ruicar.afs.cloud.common.core.constant.SecurityConstants;
import com.ruicar.afs.cloud.common.core.util.IResponse;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @Author Ice
 * @Date 2020/6/9
 */
@Slf4j
@Service
@AllArgsConstructor
public class CommonServiceImpl implements CommonService {
    private final ChannelUseApplyService channelUseApplyService;
    private final ChannelUseCaseService channelUseCaseService;


    /**
     * 修改状态
     *
     * @param channelAffiliatedCondition
     * @return
     */
    public IResponse saveThirdOrNetCar(ChannelAffiliatedToCaseCondition channelAffiliatedCondition) {
        //请求头
        Map headers = makeHeader();
        //接口返回结果
        IResponse result = channelUseCaseService.saveThirdOrNetCar(channelAffiliatedCondition, headers);
        //结果解析只有成功时，方可返回正确的值
        return IResponse.success(result);
    }


    /**
     * 修改状态
     *
     * @param ids
     * @return
     */
    public IResponse upDateCaseStatusByIds(String ids) {
        //请求头
        Map headers = makeHeader();
        //接口返回结果
        JSONObject result = (JSONObject) channelUseCaseService.upDateCaseStatusByIds(ids, headers);
        //结果解析只有成功时，方可返回正确的值
        if (result.containsKey("success") && 1 == Integer.parseInt(String.valueOf(result.get("success")))) {
            ArrayList list = (ArrayList) result.get("data");
            return IResponse.success(list);
        } else {
            String message = (String) result.get("message");
            String code = (String) result.get("code");
            return IResponse.fail("code:" + code + ",message:" + message);
        }
    }

    /**
     * 获取省市
     * @param condition
     * @return
     */


    /**
     * 获取请求头
     *
     * @return
     */
    public static Map makeHeader() {
        Map headers = new HashMap<>();
        headers.put(SecurityConstants.AFS_CLEAR, SecurityConstants.AFS_CLEAR);//删除jar包自带的头
        headers.put("Content-Type", "application/json");
        return headers;
    }
}
